<?php
namespace app\track\model;
use app\base\model\BaseModel;
use think\Db;
use think\facade\Log;
/**
 * 
 */
class TimeTrackModel extends BaseModel
{
	//时间列表
	public function time_lists($data){
		$limit =50;

		
		$albumsql = Db::name('photo_album_pics')
		->alias('ap')
		->field("FROM_UNIXTIME(ap.createtime,'%Y') as year") 
		->join('photo_album pa','pa.albumid=ap.albumid')
		->where('pa.Openid',$data['Openid'])
		->fetchSql(true)
		->select();

		$list = Db::name('circle_dynamic cd')
				->field("FROM_UNIXTIME(cd.Cretetime ,'%Y') as year")
				->fetchSql(true)
				->where('cd.Openid',$data['Openid'])
				->select();


		$res = Db::name('task_complete')
		->alias('tc')
		->field("FROM_UNIXTIME(tc.Cretetime ,'%Y') as year")
		->join('task t','t.taskid=tc.taskid')
		->union($list)
		->union($albumsql)
		->where('tc.Openid',$data['Openid'])
		->group('year')
		->order('year','desc')
		->limit($limit)
		->select();
		return $res;
	}
	//时光轨迹列表
	public function track_lists($data){
		$limit =10;
		$data['page'] = (isset($data['page'])&&!empty($data['page']))?$data['page']:1;
		$pre = ($data['page']-1)*$limit;

		$data['year'] = (isset($data['year'])&&!empty($data['year']))?$data['year']:date('Y');
		$data['month'] = (isset($data['month'])&&!empty($data['month']))?$data['month']:date('m');
		$start = strtotime($data['year']."-".$data['month']."-01");
		$m=$data['month']+1;
		$end = strtotime($data['year']."-".$m."-01");
		
		$albumsql = Db::name('photo_album_pics')
		->alias('ap')
		->field('ap.albumid as id,ap.picsid as id2,ap.createtime as Time,pa.albumname as name,ap.tracktype as tracktype') 
		->join('photo_album pa','pa.albumid=ap.albumid')
		->where('pa.Openid',$data['Openid'])
		->where('ap.createtime','gt',$start)
		->where('ap.createtime','lt',$end)
		->order('Time','desc')
		->group('Time')
		->fetchSql(true)
		->select();

		$list = Db::name('circle_dynamic cd')
				->field('cd.Dynamicid as id,cd.circleid as id2,cd.Cretetime as Time,cd.Dynamictitle as name,cd.tracktype as tracktype')
				->fetchSql(true)
				->where('cd.Openid',$data['Openid'])
				->where('cd.Cretetime','gt',$start)
				->where('cd.Cretetime','lt',$end)
				->order('Time','desc')
				->select();


		$res = Db::name('task_complete')
		->alias('tc')
		->field('tc.completeid as id,tc.taskid as id2,tc.Cretetime as Time,t.taskname as name,tc.tracktype as tracktype')
		->join('task t','t.taskid=tc.taskid')
		->union($list)
		->union($albumsql)
		->where('tc.Openid',$data['Openid'])
		->where('tc.Cretetime','gt',$start)
		->where('tc.Cretetime','lt',$end)
		->order('Time','desc')
		->limit($pre,$limit)
		->select();
		foreach ($res as $k => $v) {
			switch ($v['tracktype']) {
				case '1'://相册类型
					$res[$k]['data']=Db::name('photo_album_pics')->field('Picspath as image')->where('createtime',$v['Time'])->limit(3)->select();
					break;
				case '2'://圈子类型
				$res[$k]['data']=Db::name('circle_dynamic_pics')->field('Imgpath as image')->where('Dynamicid',$v['id'])->limit(3)->select();
				break;
				default:
					break;
			}
		
		}
		return $res;
	}
	//发布时光
	public function publish($data){
		$desc['describe']=$data['describe'];
		$desc['Token']=$data['Token'];
		$desc['Hits']=0;
		$desc['Openid']=$data['Openid'];
		$desc['Isuse']=0;
		$desc['Userid']=$data['Userid'];
		$desc['createtime']=time(); 
		$pics=$data['image'];
		// 启动事务
			Db::startTrans();
		try {
		
		    $number = Db::name('time_track')->insertGetId($desc);
		   // log::write($number);
		    foreach ($pics as $key => $value) {
		    	$pics[$key]['trackid']=$number;
		    	
		    }
		    $picnumber = Db::name('time_track_pics')->insertAll($pics);
		    // 提交事务
		    Db::commit();
		    return 1;
		} catch (\Exception $e) {
		    // 回滚事务
		    Db::rollback();
		    return 0;
		}
		
		
		
	}


	//时光列表 时间、评论数、描述、图片地址
	
	public function track_list($data,$where,$page=1,$limit=10){
		$pre = ($page-1)*$limit;
		$res = array();
		$cursor = Db::name('time_track')->alias('t')->field('t.describe,t.createtime,t.trackid,(select count(*) from xj_time_track_msg as c where c.trackid = t.trackid) as count,(select count(*) from xj_track_point as p where p.trackid = t.trackid) as point')->where($where)->order('trackid','desc')->limit($pre,$limit)->cursor();
		foreach($cursor as $k =>$v){
			$res['describe']=$v['describe'];
			$res['createtime']=date('y-m-d H:m:s',$v['createtime']);
			$res['count']=$v['count'];

			$res['image'] = Db::name('time_track_pics')->field('Picspath')->where('trackid',$v['trackid'])->limit(0,9)->order('picsid','desc')->select();
	
		}
		//dump($res);
		return $res;
	

	}
	//删除时光轨迹
	public function del_track($where,$is_delete){
		if($is_delete){
			$comment = $this->deleteByWhere('timr_track_picmsg',$where);//删除评论
			$point = $this->deleteByWhere('track_point',$where);//删除点赞
			$res = $this->deleteByWhere('time_track_pics',$where);
			$res = $this->deleteByWhere('time_track',$where);
		}else{
			$data['status'] = 2;
			$res = $this->updateByWhere('time_track',$where,$data);
		}
		return $res;

	}

		//时光评论列表
	
	public function comment_list($data,$page=1,$limit=10){
		if(empty($data['limit'])) $limit=10;
			if(empty($data['page'])||$data['page']<=0) $page=1;
			$limit = $data['limit'];
			 $pre = ($page-1)*$limit;
			$res=Db::name('time_track_msg')->alias('pm')->field('pm.cmsgid,pm.content,pm.createtime,u.wxnickname as name,r.wxnickname as replayname,u.UserImg as userimg,r.UserImg as replayimg')->join('userinfo u','u.Userid=pm.Userid','left')->join('userinfo r','pm.Replayid=r.Userid','left')->where('pm.trackid',$data['trackid'])->order('pm.createtime','desc')->limit($pre,$limit)->select();
			return $res;
		
	
	}



		//增加时光评论
	public function comment($data){
		$res = $this->insert('time_track_msg',$data);
		return $res;
	}

	//增加点赞
	public function add_points($data){
		$res = $this->insert('track_point',$data);
		return $res;
	}

	//取消点赞
	

	




	
}